package com.aofeng.hybridwork.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.aofeng.hybrid.activity.HybridActivity;
import com.aofeng.hybrid.plugin.H5Param;
import com.aofeng.hybrid.sync.SyncDBAndPagesTask;
import com.aofeng.hybrid.sync.IProgressNotifier;
import com.aofeng.hybrid.sync.SyncParam;
import com.aofeng.hybrid.util.CommUtil;
import com.aofeng.hybridwork.R;
import com.aofeng.hybridwork.android.ChengRanNativeMethod;
import com.aofeng.hybridwork.android.NativePage1Method;
import com.aofeng.hybridwork.application.HybridApplication;

/**
 *
 * 引导页
 *
 */
public class PreludeActivity extends Activity implements IProgressNotifier {

	private static final String TAG = PreludeActivity.class.getCanonicalName();

	ProgressDialog pd;

    TextView hint;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.guide);
		pd = new ProgressDialog(this);
		pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		pd.setCancelable(false);
        hint = (TextView) this.findViewById(R.id.tvHint);
	}


	@Override
	protected void onResume() {
		super.onResume();
        hint.setText("");
     	updateSystem();
    }


	/**
	 * 系统更新
	 */
	private void updateSystem() {
		String strDt = CommUtil.getPreference(this, HybridApplication.APP_ID, SyncDBAndPagesTask.UPDATE_MOST_RECENT_TIME);
		SyncDBAndPagesTask task = new SyncDBAndPagesTask(PreludeActivity.this,PreludeActivity.this);

		SyncParam sp = new SyncParam(
				HybridApplication.APP_ID,
				HybridApplication.DB_META_SERVICE_URL,
				"t_check_plan,t_aboutuser,t_equipmentinspection,t_hinvestigation",
                HybridApplication.DIR_LIST_SERVICE_URL,
                HybridApplication.REMOTE_WWW_PREFIX,
                HybridApplication.LOCAL_PATH_PREFIX,
                HybridApplication.REMOTE_BZ_PREFIX,
                HybridApplication.FILE_SERVICE_URL,
                HybridApplication.UPDATE_SERVICE_URL,
                strDt,
				HybridApplication.WWW_BASE
		);

        SyncParam[] params = new SyncParam[] {sp};

		task.execute(params);
	}


	@Override
	public void notifyProgress(int progress) {
		pd.setProgress(progress);
	}

	@Override
	public void notifyDone(boolean done) {
		pd.hide();
     	Log.d(TAG, "系统更新" + (done ? "成功" : "失败"));
		String strDt = CommUtil.getPreference(this, HybridApplication.APP_ID, SyncDBAndPagesTask.UPDATE_MOST_RECENT_TIME);
        if(!done) {
            hint.setText("系统更新失败，请检查网络是否正常。");
        }
		if(strDt == null)
			return;

		Intent intent = new Intent(this, HybridActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("param", new H5Param("file:///data/data/com.aofeng.hybridwork/files/www/index.html", ChengRanNativeMethod.class.getName()));
        intent.putExtras(bundle);
		startActivity(intent);
	}

	@Override
	public void prelude() {
		pd.setMessage("检查系统更新......");
		pd.setProgress(0);
		pd.show();
	}

}
